package com.im.app.processor;


import com.im.app.model.ChatLogs;
import com.im.app.model.Conversations;
import com.im.app.service.ChatLogsService;
import com.im.app.service.ConversationsService;
import com.im.app.utils.uuid.IdUtils;
import org.jim.core.ImChannelContext;
import org.jim.core.packets.ChatBody;
import org.jim.core.utils.JsonKit;
import org.jim.server.processor.chat.BaseAsyncChatMessageProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.UUID;

@Component
public class ChatMessageProcessor extends BaseAsyncChatMessageProcessor {

    private static Logger logger = LoggerFactory.getLogger(ChatMessageProcessor.class);
    public static ChatMessageProcessor cmp;

    @Autowired
    private ChatLogsService chatLogsService;

    @Autowired
    private ConversationsService conversationsService;

    @PostConstruct
    public void init() {
        cmp = this;
        cmp.chatLogsService = this.chatLogsService;
        cmp.conversationsService = this.conversationsService;

    }

    @Override
    public void doProcess(ChatBody chatBody, ImChannelContext imChannelContext){
        //保存消息记录
        ChatLogs chatLogs = new ChatLogs();
        chatLogs.setServerMsgId(IdUtils.simpleUUID());
        chatLogs.setClientMsgId(IdUtils.simpleUUID());
        chatLogs.setContentType(chatBody.getMsgType());
        chatLogs.setSendId(chatBody.getFrom());
        chatLogs.setCreateTime(new Date(chatBody.getCreateTime()));
        chatLogs.setContent(chatBody.getContent());
        chatLogs.setSendTime(new Date(chatBody.getCreateTime()));
        //消息类型 群聊/私聊 2-私聊 1-群聊
        chatLogs.setSessionType(chatBody.getChatType());
        if(chatBody.getChatType() == 1){
            //设群id
            chatLogs.setRecvId(chatBody.getGroupId());
        }else{
            chatLogs.setRecvId(chatBody.getTo());
        }
        cmp.chatLogsService.insert(chatLogs);

        //更新会话记录，根据通道ownerUserId和userId或群Id查询会话是否存在 不存在就创建，存在就更新
        if(chatBody.getChatType() == 1){
            //群聊会话
            Conversations groupC = conversationsService.selectByGroup(chatBody.getFrom(), chatBody.getGroupId());
            if (null != groupC){
                groupC.setLastContent(chatBody.getContent());
                groupC.setLastType(chatBody.getMsgType());
                groupC.setLastTime(new Date(chatBody.getCreateTime()));
                cmp.conversationsService.updateByGroup(groupC);
            }else{
                Conversations groupConversations = new Conversations();
                groupConversations.setOwnerUserId(chatBody.getFrom());
                groupConversations.setConversationId(UUID.randomUUID().toString());
                groupConversations.setGroupId(chatBody.getGroupId());
                groupConversations.setLastContent(chatBody.getContent());
                groupConversations.setLastType(chatBody.getMsgType());
                groupConversations.setLastTime(new Date(chatBody.getCreateTime()));
                cmp.conversationsService.insert(groupConversations);
            }
        }else{
            //私聊会话
            Conversations conversationsAb = cmp.conversationsService.selectByUser(chatBody.getFrom(), chatBody.getTo());
            Conversations conversationsBa = cmp.conversationsService.selectByUser(chatBody.getTo(), chatBody.getFrom());
            if(null!=conversationsAb && null!=conversationsBa){
                //会话已存在,更新未读数+1
                conversationsBa.setLastContent(chatBody.getContent());
                conversationsBa.setLastType(chatBody.getMsgType());
                conversationsBa.setLastTime(new Date(chatBody.getCreateTime()));
                conversationsBa.setUnreadCount(conversationsBa.getUnreadCount()+1);
                cmp.conversationsService.updateByUser(conversationsBa);
            }else {
                //创建两条会话,分别为发送方和接受方
                Conversations conversationsA = new Conversations();
                conversationsA.setOwnerUserId(chatBody.getFrom());
                conversationsA.setConversationId(UUID.randomUUID().toString());
                conversationsA.setUserId(chatBody.getTo());
                //发送方设置为0
                conversationsA.setUnreadCount(0);
                cmp.conversationsService.insert(conversationsA);
                Conversations conversationsB = new Conversations();
                conversationsB.setOwnerUserId(chatBody.getTo());
                conversationsB.setConversationId(UUID.randomUUID().toString());
                conversationsB.setUserId(chatBody.getFrom());
                //接受方设置为1
                conversationsB.setUnreadCount(1);
                conversationsB.setLastContent(chatBody.getContent());
                conversationsB.setLastType(chatBody.getMsgType());
                conversationsB.setLastTime(new Date(chatBody.getCreateTime()));
                cmp.conversationsService.insert(conversationsB);
            }

        }
        logger.info("默认交由业务处理聊天记录示例,用户自己继承BaseAsyncChatMessageProcessor即可:{}", JsonKit.toJSONString(chatBody));
    }

}
